package xyz.scootaloo.oj.leetcode

import cn.hutool.core.lang.Console
import org.junit.Test

/**
 * @author flutterdash@qq.com
 * @since 2021/4/30 15:00
 */
class P347 {

    fun topKFrequent(nums: IntArray, k: Int): IntArray {
        val map = HashMap<Int, Int>()
        for (i in nums.indices) {
            map[nums[i]] = (map[nums[i]] ?: 0) + 1
        }
        val list = ArrayList<Pair<Int, Int>>()
        for ((num, count) in map) {
            list.add(num to count)
        }
        list.sortBy { pair -> -pair.second }
        val res = IntArray(k)
        for (i in 0 until k) {
            res[i] = list[i].first
        }
        return res
    }

    @Test
    fun test0() {
        val arr0 = intArrayOf(1, 1, 1, 2, 2, 3)
        Console.log(topKFrequent(arr0, 2))
    }

    @Test
    fun test1() {
        val arr0 = intArrayOf(1)
        Console.log(topKFrequent(arr0, 1))
    }

    @Test
    fun test2() {
        val arr0 = intArrayOf(-1, -1)
        Console.log(topKFrequent(arr0, 1))
    }

}
